﻿window.top.ngW8.$NS("unvs.ui.kendo")["menu"] = function ($tmp) {
    $tmp.$a.$observe("source", function (v) {
        var data = $tmp.$s.$eval(v);
        if (angular.isUndefined(data)) return;

        if ($tmp.kendoMenu) {
            $tmp.kendoMenu.destroy();
        }
        $($tmp.$e[0]).empty();
        try {
            for (var i = 0; i < data.length; i++) {
                var li = $("<li></li>").appendTo($($tmp.$e[0])).html(data[i].text);
                li.data().dataItem = data[i];
                if (data[i].items) {
                    $tmp.install(data[i].items, li);
                }
            }
        } catch (e) {
            throw ("init data menu ", e);
        }
        $tmp.kendoMenu = $($tmp.$e[0]).kendoMenu({
            select: function (e) {
                var dataItem = $(e.item).data().dataItem;
                if ($tmp.$a.selectedItem) {
                    ngW8.$("$parse")($tmp.$a.selectedItem).assign($tmp.$s, dataItem);
                }
                if ($tmp.$a.onSelect) {
                    $tmp.$s.$eval($tmp.$a.onSelect);
                }
                $tmp.$s.$apply();
            }
        }).data("kendoMenu");
    });
    $tmp.install = function (data, li) {
        var ul = $("<ul></ul>").appendTo(li[0]);
        for (var i = 0; i < data.length; i++) {
            var subLi = $("<li></li>").appendTo(ul[0]).html(data[i].text);
            subLi.data().dataItem = data[i];
            if (data[i].items) {
                $tmp.install(data[i].items, subLi);
            }
        }
    };

}
window.top.ngW8.$NS("unvs.ui.kendo")["view"] = function ($tmp) {
    $tmp.$stack = [];
    $tmp.$a.$observe("page", function (value) {
        ngW8.$("$page")(value).$whenComplete(function (sender) {
            if ($tmp.$currentPage) {
               
                $tmp.$currentPage.$element.hide();

            }
            $tmp.$currentPage = {
                $element: sender.element,
                $scope: sender.scope,
                $pageName:value
            };
            $tmp.$stack.push($tmp.$currentPage);
            sender.element.appendTo($($tmp.$e[0])[0]);
        })
            .$whenError(function (sender) {
                $($tmp.$e[0]).html(sender.$error.data);
                if ($tmp.$masterElement) {

                }
            }).$apply($tmp.$s);
    });
};
window.top.ngW8.$NS("unvs.ui.kendo")["window"] = function ($tmp) {
    $tmp.$s.$root.$window = function (owner, path,callback) {
        ngW8.$("$page")(path).$whenComplete(function (sender) {

            sender.element.appendTo("body").kendoWindow({
                title: sender.element.attr("title")
            });
            if (callback) {
                callback(sender.scope);
            }
        })
        .$whenError(function (sender) {
            $($tmp.$e[0]).html(sender.$error.data);
            if ($tmp.$masterElement) {

            }
        }).$apply(owner);
    }
};